<template>
	<view class="">
		<view class="form-body">
			<u-form labelPosition="left" :model="formData" :rules="rules" ref="form" labelWidth="auto"
				errorType="toast">
				<u-form-item label="分类图标" borderBottom>
					<view class="flex justify-end">
						<view class="">
							<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" multiple
								:maxCount="1"></u-upload>
						</view>
					</view>
				</u-form-item>
				<u-form-item label="分类名称" prop="name" borderBottom>
					<u-input v-model="formData.name" border="none" placeholder="建议输入12字以内" inputAlign="right"></u-input>
				</u-form-item>
				<u-form-item label="会员共享码" prop="sharingCode" borderBottom>
					<u-input v-model="formData.sharingCode" border="none" placeholder="建议输入20字符" inputAlign="right" maxlength="20"></u-input>
				</u-form-item>
				<u-form-item label="排序" prop="sort" borderBottom>
					<u-input v-model="formData.sort" border="none" inputAlign="right" type="number"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<view class="bottom-button">
			<u-button type="primary" size="large" color="#e54d42" text="确定" @click="onSubmit"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isEdit: false,
				fileList: [],
				formData: {
					imageUrl:'',
					name: '',
					sharingCode:'',
					sort: 0,
					isShow: true,
				},
				rules: {
					name: [{
						required: true,
						message: '请输入项目名称',
						trigger: ['change', 'blur']
					}],
					imageUrl: [{
						required: true,
						message: '请上传',
						trigger: ['change', 'blur']
					}]
				},
			}
		},
		onLoad(option) {
			let that = this
			if (option.id) {
				uni.setNavigationBarTitle({ title: '编辑分类', })
				this.isEdit = true
				this.getEdit(option.id)
			} else {
				uni.setNavigationBarTitle({ title: '新增分类' })
				this.isEdit = false
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			getEdit(id){
				let that = this
				this.$reqJsonPost('UserCategoryGetEdit',{ id:id }).then(res=>{
					this.formData = res.data
					this.fileList.push({ url:res.data.imageUrl })
				
				})
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
				this.formData.imageUrl = ''
			},
			onSubmit() {
				this.$refs.form.validate().then(res => {
					if(this.isEdit){
						this.$reqJsonPost('UserCategoryDoEdit',this.formData).then(res=>{
							uni.$emit('upUserCategoryList')
							uni.navigateBack()
						})
					}else {
						this.$reqJsonPost('UserCategoryDoCreate',this.formData).then(res=>{
							uni.$emit('upUserCategoryList')
							uni.navigateBack()
						})
					}
					
				})
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map(item => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.$reqUpLoad(lists[i].url)
					let item = this.fileList[fileListLen]
					this.formData.imageUrl = result.fileUrl
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.fileUrl
					}))
					fileListLen++
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.form-body {
		padding: 20rpx;
		background-color: #fff;
	}
</style>
